import { Component } from '@angular/core';
import { Storage } from '@ionic/storage';
import{EventService}from'../services/event.service';
import { AlertController } from '@ionic/angular';

@Component({
  selector: 'app-tab1',
  templateUrl: 'tab1.page.html',
  styleUrls: ['tab1.page.scss']
})
export class Tab1Page {

  public list:any=[];

  constructor(private storage: Storage, public eventService: EventService,public alertController: AlertController ) {}
  
  ngOnInit(){
    this.updateNotes();
    
    this.eventService.event.on('update', () => {
      this.updateNotes();
    })
    
  }

  updateNotes(){
    this.list = [];
    this.storage.forEach((value: any, key: string)=>{
      if(value['title']==null && value['content'] == null){
        
      }else{

        this.list.push(value);
      }
    });
  }

  async doPress(key:string){
    console.log(key);

    const alert = await this.alertController.create({
      header: '提示!',
      message: '确定要删除吗!!!',
      backdropDismiss:false,
      buttons: [
        {
          text: '取消',
          role: 'cancel',
          cssClass: 'secondary',
          handler: (blah) => {
            console.log('Confirm Cancel: blah');
          }
        }, {
          text: '确定',
          handler: () => {
            this.storage.remove(key);
            this.eventService.event.emit('update');
            console.log('Confirm Okay');
          }
        }
      ]
    });

    await alert.present();
  }

  login(){
    console.log('登录')
  }
}
